<template>
  <div class="app-container">
    <el-button
      icon="el-icon-plus"
      type="primary"
      @click="showAdd"
    >{{ $t('add') }}</el-button>
    <el-table
      :data="serviceLinkList"
      style="width: 100%"
      @cell-click="open"
    >
      <el-table-column
        prop="name"
        :label="$t('name')"
      />
      <el-table-column
        :label="$t('url')"
      >
        <template slot-scope="scope">
          <el-link v-text="scope.row.url" />
        </template>
      </el-table-column>
      <el-table-column width="210" :label="$t('function')" >
        <template slot-scope="scope">
          <el-button
            :disabled="scope.row.brand"
            type="text"
            icon="el-icon-edit"
            @click.stop="edit(scope.row)"
          >{{ $t('edit') }}
          </el-button>
          <el-button
            :disabled="scope.row.brand"
            style="color: #f56c6c"
            type="text"
            icon="el-icon-delete"
            @click.stop="del(scope.row)"
          >{{ $t('delete') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      :visible.sync="dialogFormVisible"
    >
      <el-form
        label-width="120px"
        :model="serviceLink"
      >
        <el-form-item :label="$t('name')">
          <el-input v-model="serviceLink.name" />
        </el-form-item>
        <el-form-item :label="$t('url')">
          <el-input v-model="serviceLink.url" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">{{ $t('cancel') }}</el-button>
        <el-button type="primary" @click="addOrUpdate">{{ $t('confirm') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  insertServiceLink,
  updateServiceLink,
  deleteServiceLink,
  listServiceLink
} from '@/api/iot/iotServiceLinkApi'
import { getCompanyId } from '@/util/storage'
import {
  successNotice,
  cancelNotice
} from '@/util/common'
export default {
  name: 'ServiceLink',
  data() {
    return {
      dialogFormVisible: false,
      companyId: getCompanyId(),
      serviceLink: {},
      serviceLinkList: []
    }
  },
  created() {
    this.listServiceLink()
  },
  methods: {
    edit(index) {
      this.serviceLink = index
      this.dialogFormVisible = true
    },
    open(item) {
      window.open(item.url)
    },
    del(index) {
      const that = this
      this.$confirm(that.$t('delete') + index.name, {
        confirmButtonText: that.$t('confirm'),
        cancelButtonText: that.$t('cancel'),
        type: 'warning'
      })
        .then(function() {
          deleteServiceLink({
            linkId: index.linkId
          }).then(res => {
            const { code } = res
            if (code === '200') {
              successNotice(that)
              that.listServiceLink()
            }
          })
        })
        .catch(function() {
          cancelNotice(that)
        })
    },
    listServiceLink() {
      listServiceLink({
        companyId: this.companyId
      }).then(res => {
        const { code, data } = res
        if (code === '200') {
          this.serviceLinkList = data
        } else {
          this.serviceLinkList = []
        }
      })
    },
    showAdd() {
      this.serviceLink = {
        companyId: this.companyId
      }
      this.dialogFormVisible = true
    },
    addOrUpdate() {
      const json = {
        ...this.serviceLink
      }
      if (!this.serviceLink.linkId) {
        insertServiceLink(json).then(res => {
          const { code } = res
          if (code === '200') {
            successNotice(this)
            this.dialogFormVisible = false
          }
          this.listServiceLink()
        })
      } else {
        updateServiceLink(json).then(res => {
          const { code } = res
          if (code === '200') {
            successNotice(this)
            this.dialogFormVisible = false
          }
          this.listServiceLink()
        })
      }
    }
  }
}
</script>


